package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.domain.HcxAppUser;
import com.ruoyi.business.domain.HcxMerchant;
import com.ruoyi.business.domain.HcxMerchantOrder;
import com.ruoyi.business.mapper.HcxAppUserMapper;
import com.ruoyi.business.mapper.HcxMerchantMapper;
import com.ruoyi.business.mapper.HcxMerchantOrderMapper;
import com.ruoyi.business.mapper.HcxMerchantTypeMapper;
import com.ruoyi.business.service.IHcxStatisticsService;
import com.ruoyi.business.vo.HcxDateCountVo;
import com.ruoyi.business.vo.HcxTongjiSjflVo;
import com.ruoyi.common.constant.Constants;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class HcxStatisticsServiceImpl implements IHcxStatisticsService {

    private final HcxAppUserMapper hcxAppUserMapper;

    private final HcxMerchantMapper hcxMerchantMapper;

    private final HcxMerchantOrderMapper hcxMerchantOrderMapper;

    private final HcxMerchantTypeMapper hcxMerchantTypeMapper;

    @Override
    public Map<String, Object> tongji() {


        Map<String, Object> result = new HashMap<>();
        // 查询app注册用户数量
        Integer userCount = hcxAppUserMapper.selectCount(Wrappers.emptyWrapper());
        // 商家入驻数量
        Integer merchantCount = hcxMerchantMapper.selectCount(Wrappers.emptyWrapper());
        // 订单数量
        Integer orderCount = hcxMerchantOrderMapper.selectCount(Wrappers.emptyWrapper());
        // 订单金额
        BigDecimal orderPrice = hcxMerchantOrderMapper.selectList(Wrappers.emptyWrapper()).stream().map(HcxMerchantOrder::getActualPrice).reduce(BigDecimal.ZERO, BigDecimal::add);


        // 查询最近7天的用户注册数量
        List<HcxDateCountVo> userDateCount = hcxAppUserMapper.last7Days();

        // 查询最近7天的商家入驻数量
        List<HcxDateCountVo> merchantDateCount = hcxMerchantMapper.last7Days();

        // 最近7天的订单数量和金额
        List<HcxDateCountVo> orderDateCount = hcxMerchantOrderMapper.last7Days();

        // 最近7天的日期
        List<String> last7Days = userDateCount.stream().map(HcxDateCountVo::getCreateTime).collect(Collectors.toList());

        // 用户数量
        List<String> userList = userDateCount.stream().map(HcxDateCountVo::getCount).collect(Collectors.toList());
        // 商家数量
        List<String> merchantList = merchantDateCount.stream().map(HcxDateCountVo::getCount).collect(Collectors.toList());
        // 订单数量
        List<String> orderList = orderDateCount.stream().map(HcxDateCountVo::getCount).collect(Collectors.toList());
        // 订单金额
        List<String> orderPriceList = orderDateCount.stream().map(HcxDateCountVo::getOrderprice).collect(Collectors.toList());


        // 商家分类信息
        Map<String, Long> typeMap = hcxMerchantMapper.selectList(Wrappers.emptyWrapper()).stream().collect(Collectors.groupingBy(HcxMerchant::getTypeName, Collectors.counting()));
        List<HcxTongjiSjflVo> nameAndValueList = typeMap.entrySet().stream().map(e -> new HcxTongjiSjflVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        // 分类名称
        List<String> typeNameList = typeMap.keySet().stream().collect(Collectors.toList());


        // 会员人数
        Integer vipNumber = hcxAppUserMapper.selectCount(new LambdaQueryWrapper<HcxAppUser>().eq(HcxAppUser::getVipFlag, Constants.ONE));


        List<List<String>> list = new ArrayList<>();
        list.add(userList);
        list.add(merchantList);
        list.add(orderList);
        list.add(orderPriceList);

        result.put("userCount", userCount);
        result.put("merchantCount", merchantCount);
        result.put("orderCount", orderCount);
        result.put("orderPrice", orderPrice);

        result.put("last7Days", last7Days);
        result.put("dataList", list);
        result.put("nameAndValueList", nameAndValueList);
        result.put("typeNameList", typeNameList);

        result.put("vipNumber", vipNumber);
        return result;
    }
}
